﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Data;  

namespace RealEstateCenter
{
    public partial class AddEstate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            new login_check("estate_center");
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            System.Drawing.Image FullsizeImage = System.Drawing.Image.FromStream(estate_image.FileContent);
            float ratio = (float)FullsizeImage.Height / (float)FullsizeImage.Width;
            System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(300, (int)(ratio * 300), null, IntPtr.Zero);
            MemoryStream imgstream = new MemoryStream();
            NewImage.Save(imgstream, System.Drawing.Imaging.ImageFormat.Jpeg);
            FullsizeImage.Dispose();
            NewImage.Dispose();
            imgstream.Position = 0;
            byte[] imgbyte = new byte[imgstream.Length + 1];
            imgstream.Read(imgbyte, 0, imgbyte.Length);
            //
            SqlConnection sqlconnect = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            //SqlCommand cmd = new SqlCommand("insert into Estate(estate_address, estate_city, estate_room_number, estate_description, estate_price, estate_heating_type, estate_type, estate_floor, estate_image) values('"+estate_address.Text+"','"+estate_city.Text+"','"+estate_room_number.Text+"','"+estate_description.Text+"','"+estate_price.Text+"','"+estate_heating_type.Text+"','"+estate_type.Text+"','"+estate_floor.Text+"','"+imgbyte+"')", sqlconnect);
            SqlCommand insertCommand = new SqlCommand("INSERT into Estate values (@estate_address,@estate_city,@estate_room_number,@estate_description,@estate_price,@estate_heating_type,@estate_type,@estate_floor,@estate_agency_id,@estate_image)", sqlconnect);
            insertCommand.Parameters.Add("@estate_address", SqlDbType.VarChar, 150);
            insertCommand.Parameters.Add("@estate_city", SqlDbType.VarChar, 50);
            insertCommand.Parameters.Add("@estate_room_number", SqlDbType.VarChar, 10);
            insertCommand.Parameters.Add("@estate_description", SqlDbType.VarChar, 360);
            insertCommand.Parameters.Add("@estate_price", SqlDbType.Int);
            insertCommand.Parameters.Add("@estate_heating_type", SqlDbType.VarChar, 20);
            insertCommand.Parameters.Add("@estate_type", SqlDbType.VarChar, 20);
            insertCommand.Parameters.Add("@estate_floor", SqlDbType.Int);
            insertCommand.Parameters.Add("@estate_agency_id", SqlDbType.Int);
            insertCommand.Parameters.Add("@estate_image", SqlDbType.Image);
            insertCommand.Parameters["@estate_address"].Value = estate_address.Text;
            insertCommand.Parameters["@estate_city"].Value = estate_city.Text;
            insertCommand.Parameters["@estate_room_number"].Value = estate_room_number.Text;
            insertCommand.Parameters["@estate_description"].Value = estate_description.Text;
            insertCommand.Parameters["@estate_price"].Value =  Convert.ToInt32(estate_price.Text.ToString());
            insertCommand.Parameters["@estate_heating_type"].Value = estate_heating_type.Text;
            insertCommand.Parameters["@estate_type"].Value = estate_type.Text;
            insertCommand.Parameters["@estate_floor"].Value = Convert.ToInt32(estate_floor.Text.ToString());
            insertCommand.Parameters["@estate_agency_id"].Value = 0;
            insertCommand.Parameters["@estate_image"].Value = imgbyte;
            sqlconnect.Open();
            insertCommand.ExecuteNonQuery();
            int kontrol = 1;
            sqlconnect.Close();
            if (kontrol == 1)
            {
                Response.Redirect("ListEstates.aspx");
            }
        }
    }
}